//package com.gk.study.interceptor;
//
//import com.google.gson.Gson;
//import com.gk.study.common.APIResponse;
//import com.gk.study.common.ResponeCode;
//import com.gk.study.entity.OpLog;
//import com.gk.study.entity.User;
//import com.gk.study.permission.Access;
//import com.gk.study.permission.AccessLevel;
//import com.gk.study.service.OpLogService;
//import com.gk.study.service.UserService;
//import com.gk.study.utils.IpUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpHeaders;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.lang.reflect.Method;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
///**
// * 该拦截器有两个用途：1.拦截request记录log 2.接口验权
// */
//@Component
//public class AccessInterceptor extends HandlerInterceptorAdapter {
//
//    private Logger logger = LoggerFactory.getLogger(AccessInterceptor.class);
//
//    private static OpLogService service;
//
//    private static UserService userService;
//
//    @Autowired
//    public void setOpLogService( OpLogService service) {
//        // 为解决先@Component 后@Autowired失效的方案
//        AccessInterceptor.service = service;
//    }
//
//    @Autowired
//    public void setUserService( UserService userService) {
//        // 为解决先@Component 后@Autowired失效的方案
//        AccessInterceptor.userService = userService;
//    }
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
//            throws Exception {
//
//        request.setAttribute("_startTime", System.currentTimeMillis());
//
//        //**********************验权代码*************************
//        if (!(handler instanceof HandlerMethod)) {
//            return true;
//        }
//        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        Method method = handlerMethod.getMethod();
//        Access access = method.getAnnotation(Access.class);
//        if (access == null) {
//            // 如果注解为null, 说明不需要拦截, 直接放过
//            return true;
//        }
//
//        return true;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//        //**********************记录log代码*************************
//        Long endTime = System.currentTimeMillis();
//        Long startTime = (Long) request.getAttribute("_startTime");
//        Long diff = (endTime - startTime);
//
//        OpLog opLog = new OpLog();
//        opLog.setReIp(IpUtils.getIpAddr(request));
//        opLog.setReMethod(request.getMethod());
//        opLog.setReUrl(request.getRequestURI());
//        opLog.setReUa(request.getHeader(HttpHeaders.USER_AGENT));
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//        opLog.setReTime(formatter.format(new Date()));
//        opLog.setAccessTime(String.valueOf(diff));
//        service.createOpLog(opLog);
//    }
//
//    public void writeResponse(HttpServletResponse response, APIResponse apiResponse) throws IOException {
//        response.setStatus(200);
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json");
//        Gson gson = new Gson();
//        String jsonStr = gson.toJson(apiResponse);
//        response.getWriter().println(jsonStr);
//        response.getWriter().flush();
//    }
//}
